import { defineStore } from 'pinia';
import type { CityStoreState } from '@/types/store';
import cityStoreStorage from '@/utils/cityStoreStorage';

export const useCityStore = defineStore('city', {
  state: (): CityStoreState => cityStoreStorage.get(),
  actions: {
    setCityId(cityId: string) {
      this.cityId = cityId;
    },
    setCityName(cityName: string) {
      this.cityName = cityName;
    },
    setField(field: string) {
      this.field = field;
    },
    initStore() {
      const initState = cityStoreStorage.init();

      for (let k in this.$state) {
        this.$state[k as keyof CityStoreState] = initState[k as keyof CityStoreState];
      }
    },
    syncStore() {
      cityStoreStorage.set(this.$state);
    },
  },
});
